package com.wsz.logistic.controller.admin;

import com.wsz.logistic.bean.Order;
import com.wsz.logistic.bean.OrderStatus;
import com.wsz.logistic.bean.Type;
import com.wsz.logistic.common.util.R;
import com.wsz.logistic.service.OrderService;
import com.wsz.logistic.service.UserinfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/admin/order")
public class OrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private UserinfoService userinfoService;


    @GetMapping("/list")
    public String list() {
        return "admin/order/order-list";
    }

    @GetMapping("/kind")
    public String kind() {
        return "admin/order/order-kind";
    }

    @GetMapping("/page")
    @ResponseBody
    public R page(int page, int limit, Order order) {
        return orderService.getPage(page, limit, order);
    }

    @GetMapping("/edit")
    public String edit(Integer orderId, Model model) {
        if (orderId != null) {
            model.addAttribute("order", orderService.getById(orderId));
        }
        return "admin/order/order-update";
    }

    @GetMapping("/status")
    public String status(Integer orderId, Integer deliverierId, HttpSession session) {
        if (orderId != null) {
            OrderStatus orderStatus = new OrderStatus();
            orderStatus.setDeliverierId(deliverierId);
            orderStatus.setOrderId(orderId);
            userinfoService.setOrderStatus(orderStatus);
            session.setAttribute("orderId", orderId);
            System.out.println(deliverierId + "");
        }
        return "admin/order/order-status";
    }

    @GetMapping("/changestatus")
    @ResponseBody
    public R cahangeStatus(Order order, HttpSession session) {
        order.setOrderId((Integer) session.getAttribute("orderId"));
        orderService.saveOrUpdate(order);
        return R.ok();
    }

    @PostMapping(value = {"/update", "/password"})
    @ResponseBody
    public R update(Order order) {
        orderService.saveOrUpdate(order);
        return R.ok();
    }

    @DeleteMapping("/delete/{orderId}")
    @ResponseBody
    public R delete(@PathVariable int orderId) {
        orderService.deleteById(orderId);
        return R.ok();
    }

    @GetMapping("/my-deliverier")
    @ResponseBody
    public R getMyList(int page, int limit, int deliverierId) {
        return orderService.getMyList(page, limit, deliverierId);
    }


    @GetMapping("/province")
    @ResponseBody
    public R getProvince() {
        return orderService.getProvince();
    }

    @GetMapping("/city/{provinceId}")
    @ResponseBody
    public R getCity(@PathVariable int provinceId) {
        return orderService.getCity(provinceId);
    }

    @GetMapping("/district/{cityId}")
    @ResponseBody
    public R getDistrict(@PathVariable int cityId) {
        return orderService.getDistrict(cityId);
    }

    @GetMapping("/district")
    @ResponseBody
    public R getDistrictEA() {
        return orderService.getDistrict();
    }


    @GetMapping("/type")
    @ResponseBody
    public R getType() {
        return orderService.getTypeAllEA();
    }

    @GetMapping("/type/edit")
    public String editType(Integer typeId, Model model) {
        if (typeId != null) {
            model.addAttribute("type", orderService.getTypeById(typeId));
        }
        return "admin/order/order-kind-update";
    }

    @DeleteMapping("/delete/{typeId}")
    @ResponseBody
    public R deleteType(@PathVariable int typeId) {
        orderService.deleteTypeById(typeId);
        return R.ok();
    }

    @GetMapping("/type/page")
    @ResponseBody
    public R getTypePage(int page, int limit, Type type) {
        return orderService.getTypePage(page, limit, type);
    }

    @PostMapping("/type/update")
    @ResponseBody
    public R typeUpdate(Type type) {
        orderService.saveOrUpdateType(type);
        return R.ok();
    }
}

